var muitiparty = require('multiparty'),
	fs =require('fs');

module.exports.newFiles = function(req, PATH, callback, jindu){
	var form = new muitiparty.Form()
	form.parse(req, function(err, fields, files){
		if (err) {
			callback({'status':err})
		} else {
			var oldPath = files.file[0].path
			fs.readFile(oldPath, function(err, date){
				if (err) {
					console.log(err)
				} else {
					var str = oldPath
					var paths = str.match(/.{24}\.jpg|.{24}\.png|.{24}\.mp4|.{24}\.gif/)
					if (paths == null || paths == '') {
						fs.unlinkSync(oldPath)
						callback({'status':503})
					} else {
						var newPath = PATH + paths[0]
						fs.writeFile(newPath,date,function(err){
							if (err) {
								console.log(err)
								fs.unlinkSync(oldPath)
							} else {
								fs.unlinkSync(oldPath)
							}
						})
						callback({'status':200,'paths':paths[0]})
					}
				}
			})
		}
	})
	form.on('progress',function(bytesReceived, bytesExpected){
		var status = parseInt(bytesReceived/bytesExpected*100);
		// console.log(req.session)
		jindu(status)
	})
}